/*
 * bomb.c
 *
 *  Created on: 26 févr. 2013
 *      Author: Guillaume
 */

#include <time.h>
#include "../include/game.h"
#include "../include/map.h"
#include "../include/player.h"
#include "../include/misc.h"
#include "../include/level.h"
#include "../include/bomb.h"
#include "../include/list_bomb.h"

//On crée un élément de la liste chainée

// FIXME
struct s_listbomb {
	t_bomb bomb;
	t_listbomb next;
};

struct s_bomb {
	int x, y;
	int power;
	Uint32 time_init;
	int time;
	int position;
	SDL_Surface * length_img[9];
};

// FIXME
struct s_game {
	t_level curr_level; // current level
	t_player player;
	//t_bomb bomb;
	t_listbomb listbomb;

	//t-monster monster;
	SDL_Surface * number[10];
	SDL_Surface * banner_life;
	SDL_Surface * banner_bomb;
	SDL_Surface * banner_range;
	SDL_Surface * banner_line;

};

t_bomb bomb_init(int power, int x, int y,int time) {
	t_bomb bomb = malloc(sizeof(*bomb));
	if (!bomb) {
		error("Memory error");
	}
	bomb->length_img[0] = load_image(IMG_BOMB_TTL1);
	bomb->length_img[1] = load_image(IMG_BOMB_TTL2);
	bomb->length_img[2] = load_image(IMG_BOMB_TTL3);
	bomb->length_img[3] = load_image(IMG_BOMB_TTL4);
	bomb->length_img[4] = CELL_EMPTY;
	//bomb->length_img[5] = load_image(IMG_MONSTER_DOWN);
	//bomb->length_img[6] = load_image(IMG_BOMB_TTL1);
	//bomb->length_img[7] = load_image(IMG_BOMB_TTL1);
	//bomb->length_img[8] = load_image(IMG_BOMB_TTL1);
	bomb->power = power;
	bomb->x = x;
	bomb->y = y;
	bomb->time = time;
	bomb->time_init = SDL_GetTicks();
	return bomb;
}
/*
 int bomb_get_x(t_bomb bomb) {
 assert(bomb != NULL);
 return bomb->x;
 }
 int bomb_get_y(t_bomb bomb) {
 assert(bomb != NULL);
 return bomb->y;
 }
 void bomb_from_map(t_bomb bomb, t_map map, int x, int y) {
 bomb->x = x;
 bomb->y = y;
 }
 */
//void map_set_cell_type(t_bomb bomb, t_map map, int x, int y, t_cell_type CELL_BOMB ) {
//	assert(BOMB);
// }
void bomb_display(t_bomb bomb, SDL_Surface *screen,t_player player) {
	assert(bomb);
	assert(screen);
	assert(bomb->length_img[1]);
	SDL_Rect place;

	place.x = bomb->x * SIZE_BLOC;
	place.y = bomb->y * SIZE_BLOC;
// Timer
	Uint32 time_elapsed = SDL_GetTicks() - bomb->time_init;
	if (time_elapsed < 1000) {
		SDL_BlitSurface(bomb->length_img[3], NULL, screen, &place);
		bomb->time=1000;
	} else if (time_elapsed < 2000) {
		SDL_BlitSurface(bomb->length_img[2], NULL, screen, &place);
		bomb->time=2000;
	} else if (time_elapsed < 3000) {
		SDL_BlitSurface(bomb->length_img[1], NULL, screen, &place);
		bomb->time=3000;
	} else if (time_elapsed < 4000) {
		SDL_BlitSurface(bomb->length_img[0], NULL, screen, &place);
		bomb->time=4000;
		bomb->position=1;
		//bomb_explode(player,map);
	} else if (time_elapsed > 4500) {
		SDL_BlitSurface(bomb->length_img[4], NULL, screen, &place);
		bomb->time=5000;

		if(bomb->x == player_get_x(player) && bomb->position==1){
			fprintf(stderr,"Je t'ai eu mon coco\n");
			player_dec_nb_life(player);
			bomb->position=2;
		}

		else if(bomb->x == player_get_x(player)-1 && bomb->y == player_get_y(player) && bomb->position==1){
			fprintf(stderr,"Je t'ai eu mon coco\n");
			player_dec_nb_life(player);
			bomb->position=2;
		}
		else if(bomb->x == player_get_x(player) && bomb->y+1 == player_get_y(player) && bomb->position==1){
			fprintf(stderr,"Je t'ai eu mon coco\n");
			player_dec_nb_life(player);
			bomb->position=2;
		}
		else if(bomb->x == player_get_x(player)+1 && bomb->y == player_get_y(player) && bomb->position==1){
			fprintf(stderr,"Je t'ai eu mon coco\n");
			player_dec_nb_life(player);
			bomb->position=2;
		}

		bomb->position=2;



// a partir de la ca marche plus
	} /*else if (time_elapsed > 5000) {
		SDL_BlitSurface(bomb->length_img[5], NULL, screen, &place);
	} else if (time_elapsed > 5500) {
		SDL_BlitSurface(bomb->length_img[6], NULL, screen, &place);
	} else if (time_elapsed > 6000) {
		SDL_BlitSurface(bomb->length_img[7], NULL, screen, &place);
	} else if (time_elapsed > 6500) {
		SDL_BlitSurface(bomb->length_img[8], NULL, screen, &place);
	}*/
	/* Bomb power:
	 *
	 * SDL_Rect position;
	 position.x= bomb->x;
	 position.y= bomb->y;
	 SDL_FreeSurface(SDL_Rect postion);
	 position=NULL; */
}

t_bomb bomb_create(t_game game, t_map map, int power) {

	int x = player_get_x(game->player);
	int y = player_get_y(game->player);

	t_bomb bomb = bomb_init(power, x, y,4);

	// Add new bomb to the linked list
	list_add(game->listbomb, bomb);
	list_print(game->listbomb); // debug :p

	return bomb;
}



void bomb_explode(t_player player, t_map map) {

	if ( bomb_can_explode (map, player_get_x(player),player_get_y(player) + 1) == 0
			&& map_is_inside(map, player_get_x(player),player_get_y(player) + 1) ) {

		//map_set_cell_type(map, player_get_x(player), player_get_y(player) + 1,CELL_EMPTY);


		//fprintf(stderr, "avant BOMB CREATE %d\n", rand_a_b(0,1));
		if(map_is_inside(map, player_get_x(player), player_get_y(player)+1)) {
			map_set_cell_type(map, player_get_x(player), player_get_y(player)+1, CELL_BONUS);
		}

	}

	if (bomb_can_explode (map, player_get_x(player)+1,player_get_y(player)) == 0
			&& map_is_inside(map, player_get_x(player) + 1,player_get_y(player))) {

		if(map_is_inside(map, player_get_x(player)+1, player_get_y(player))) {
			map_set_cell_type(map, player_get_x(player) + 1, player_get_y(player), CELL_EMPTY);
		}


	}

	if (bomb_can_explode (map, player_get_x(player)-1,player_get_y(player) + 1) == 0
				&& map_is_inside(map, player_get_x(player) - 1,player_get_y(player)) ) {

		if(map_is_inside(map, player_get_x(player), player_get_y(player)+1)) {
			map_set_cell_type(map, player_get_x(player) - 1, player_get_y(player), CELL_EMPTY);
		}
	}

	if (map_get_cell_type(map, player_get_x(player) , player_get_y(player)-1) == 3
				&& map_is_inside(map, player_get_x(player) ,player_get_y(player)-1)) {

		if(map_is_inside(map, player_get_x(player), player_get_y(player)+1)) {
			map_set_cell_type(map, player_get_x(player) , player_get_y(player)-1, CELL_EMPTY);
		}
	}


	//fprintf(stderr, "Coucou %d\n",map_get_cell_type ( map,player_get_x(player), player_get_y(player)));

	//fprintf(stderr, "Coucou\n");

}

void take_key(t_player player, t_map map) {
	fprintf(stderr, "Ca marche %d\n",map_get_cell_type(map, player_get_x(player), player_get_y(player)));
	if(map_get_cell_type(map, player_get_x(player) , player_get_y(player)) == 0){
		if(map_is_inside(map, player_get_x(player), player_get_y(player))) {
					map_set_cell_type(map, player_get_x(player), player_get_y(player), CELL_EMPTY);
					//map_set_cell_type(map, player_get_x(player), player_get_y(player), CELL_DOOR);
					//map_get_cell_type(map, 3, 3)==map_get_cell_type(map, 3, 4);
				}
	fprintf(stderr, "Je vais prendre la clé %d\n", player_get_x(player));
	}



}


/*
void print_list_element(t_bomb bomb) {

	printf("x=%d:y=%d-> ", bomb->x, bomb->y);
}



void list_print(list l) {
	if (l) {

		print_list_element(l->bomb);

		list_print(l->next);
	}
}
*/


int bomb_can_explode (t_map map, int bombe_x, int bombe_y) {

	switch (map_get_cell_type(map, bombe_x, bombe_y)) {

	case CELL_KEY:
		return 3;
	break;

	case CELL_CASE:
		return 0;
	break;

	case CELL_EMPTY:
		return 2;
		break;

	default:
		return 1;
	}
}


int bomb_add(t_bomb bomb) {
	//uint actual=((SDL_GetTicks()-bomb->time_init));
	//if (actual >= (bomb->time)*1000 && actual <= (bomb->time)*1000+1000/30){	//because the game is update 30 times/second
		return 1;
	//}
	//return 0;
}

int bomb_damages(t_bomb bomb, t_player player, t_map map) {
/*
	if (bomb->x == player_get_x(player))
	{
		fprintf(stderr, "J'ai tué le monstre %d à la position %d\n", player_get_x(player),bomb->position);

	}
	*/
	return 1;


}

/*
 struct s_bomb {
 int bomb_x, bomb_y;
 SDL_Surface *directed_img_bomb[5];
 int timer;
 int power;

 };

 struct ListeBomb {
 t_bomb bomb;
 list next;
 };

 int list_is_empty(list l) {
 return !l;
 }


 list bomb_init()
 {
 list newlist = malloc(sizeof(*newlist));
 assert(newlist);
 newlist->bomb->bomb_x=NULL;
 newlist->bomb->bomb_y=NULL;

 newlist->bomb->timer=0;
 newlist->next=NULL;

 }
 list bomb_create(t_player player, t_map map, list newlist) {

 t_bomb bomb = malloc(sizeof(*bomb));			//And a new bomb

 int x = player_get_x(player);
 int y = player_get_y(player);

 bomb->bomb_x = x;
 bomb->bomb_y = y;

 time_t secondes;
 struct tm instant;



 // printf("%d/%d ; %d:%d:%d\n", instant.tm_mday+1, instant.tm_mon+1, instant.tm_hour, instant.tm_min, instant.tm_sec);

 newlist->bomb = bomb;
 newlist->next = NULL;
 newlist->bomb->timer = 4000;

 bomb_affichage(player, map, newlist);

 //list_print(newlist);

 //bomb_affichage(player,map,newlist);

 return newlist;
 }

 void print_list_element(t_bomb bomb) {

 printf("x=%d:y=%d; time=%d-> ", bomb->bomb_x, bomb->bomb_y, bomb->timer);
 }

 void list_print(list l) {
 if (l) {

 print_list_element(l->bomb);

 list_print(l->next);
 }
 }

 void bomber(t_player player, t_map map) {
 list newlist = malloc(sizeof(*newlist));   //We create a new list

 if (list_is_empty(newlist)) {

 }
 //print('%d',player_get_x(player));

 bomb_create(player, map, newlist);

 list_print(newlist);

 }

 void bomb_affichage(t_player player, t_map map, list listbomb) {

 while (listbomb != NULL ) {

 if (listbomb->bomb->timer > 3500) {
 map_set_cell_type(map, listbomb->bomb->bomb_x,
 listbomb->bomb->bomb_y, CELL_BOMB_TTL4);
 listbomb->bomb->timer -= 1000;

 } else if (listbomb->bomb->timer > 2500) {
 map_set_cell_type(map, listbomb->bomb->bomb_x,
 listbomb->bomb->bomb_y, CELL_BOMB_TTL1);
 listbomb->bomb->timer -= 1000;
 } else if (listbomb->bomb->timer > 1500) {
 map_set_cell_type(map, listbomb->bomb->bomb_x,
 listbomb->bomb->bomb_y, CELL_BOMB_TTL2);
 listbomb->bomb->timer -= 1000;
 } else if (listbomb->bomb->timer > 500) {
 map_set_cell_type(map, listbomb->bomb->bomb_x,
 listbomb->bomb->bomb_y, CELL_BOMB_TTL1);
 listbomb->bomb->timer -= 1000;
 } else {
 map_set_cell_type(map, listbomb->bomb->bomb_x,
 listbomb->bomb->bomb_y, CELL_BOMB_TTL1);

 }
 listbomb = listbomb->next;
 }

 }

 */
